package com.msr.controller;

import com.msr.bean.Desk;
import com.msr.bean.Food;
import com.msr.bean.FoodType;
import com.msr.service.DeskService;
import com.msr.service.FoodService;
import com.msr.service.FoodTypeService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author tom
 * @version V1.0
 * @Package com.msr.controller
 * @date 2024/1/17 10:13
 * @Copyright © 株式会社多言语系统研究所
 */
@Controller
@RequestMapping("/ui")
public class MainController {
    @Autowired
    private DeskService deskService;
    @Autowired
    private FoodService foodService;
    @Autowired
    private FoodTypeService foodTypeService;

    @RequestMapping("/main")
    public String toMain(Model model){
        //跳转到页面之前，查询所有的餐桌的数据
        List<Desk> desks = deskService.deskAll(null);

        //从上述列表中过滤出“空闲”的餐桌数据
        List<Desk> deskNull = new ArrayList<>();

        //循环查询空闲餐桌
        for (Desk desk : desks) {
            if(desk.getState() == 0){ //空闲
                deskNull.add(desk);
            }
        }

        //把空闲餐桌的列表集合保存起来
        model.addAttribute("deskNull",deskNull);

        return "main";
    }

    /**
     * 跳转到菜单页面
     * @return
     */
    @RequestMapping("/menu/{deskId}/{typeName}")
    public String menu(Model model, HttpSession session,@PathVariable("deskId") Integer deskId,@PathVariable("typeName") String typeName){
        //01-获取菜品数据
        List<Food> foods = foodService.foodAll(null);
        model.addAttribute("foods",foods);
        //02-获取菜系数据
        List<FoodType> foodTypes = foodTypeService.getFoodTypes(null);
        session.setAttribute("foodTypes",foodTypes);
        //03-获取餐桌数据
        Desk desk = deskService.findDeskById(deskId);
        model.addAttribute("desk",desk);

        //保存指定菜系对应菜品
        List<Food> foodTypeList = new ArrayList<>();
        if(!typeName.equals("全部")){  //xc
            for (Food food : foods) {
                if(food.getFoodType().getName().equals(typeName)){
                    foodTypeList.add(food);
                }
            }
            model.addAttribute("foods",foodTypeList);
        }
        return "caidan";
    }
}
